package mark.tom.friend.base.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import java.security.MessageDigest;
import java.util.List;

import static android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS;

public final class SystemUtil {

    public static List<PackageInfo> getInstalledPackages(@NonNull PackageManager packageManager) {
        return packageManager.getInstalledPackages(0);
    }

    @ColorInt
    public static int getColorByRes(@NonNull Resources res, @ColorRes int colorResId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return res.getColor(colorResId, null);
        } else {
            return res.getColor(colorResId);
        }
    }

    public static Drawable getDrawableByRes(@NonNull Resources res, @DrawableRes int drawableResId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return res.getDrawable(drawableResId, null);
        } else {
            return res.getDrawable(drawableResId);
        }
    }

    public static void hideSoftInput(View mView) {
        Context mContext = mView.getContext();
        InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (null == inputManager || !inputManager.isActive() ) {
            return;
        }
        IBinder binder = mView.getWindowToken();
        if (null == binder) {
            return;
        }
        inputManager.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public static Drawable getAppIcon(@NonNull PackageManager packageManager, @NonNull String packageName) {
        try {
            ApplicationInfo info = packageManager.getApplicationInfo(packageName, 0);
            return info.loadIcon(packageManager);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }

    public static String[] getAppSignature(@NonNull PackageManager packageManager, @NonNull String packageName) {
        String[] result = new String[2];
        try {
            @SuppressLint("PackageManagerGetSignatures") PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
            Signature[] signs = packageInfo.signatures;
            Signature sign = signs[0];
            String md5Fingerprint = parseFingerprint(sign.toByteArray(), "MD5");
            String sha1Fingerprint = parseFingerprint(sign.toByteArray(), "SHA1");
            result[0] = md5Fingerprint;
            result[1] = sha1Fingerprint;
        } catch (Exception e) {
            // Do Nothing
        }
        return result;
    }

    private static String parseFingerprint(byte[] certificateBytes, String algorithm) throws Exception {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(certificateBytes);
        byte[] digest = md.digest();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            if (i != 0) {
                sb.append(":");
            }
            int b = digest[i] & 0xff;
            String hex = Integer.toHexString(b);
            if (hex.length() == 1) {
                sb.append("0");
            }
            sb.append(hex);
        }
        return sb.toString();
    }

}
